﻿using HuaHan.WorkTicket.Domain;
using HuaHan.WorkTicket.ServiceContracts;
using KendoGridBinder;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WorkTicket.Application.Implementation;
using WorkTicket.Website.Common;

namespace WorkTicket.Website.Controllers
{
    public class EmployeeController : Controller
    {
        protected IEmployeeService employeeService = new EmployeeServiceImpl();
        //
        // GET: /Employee/

        public ActionResult Index()
        {
            return View();
        }


        public JsonResult GetAllItems(KendoGridRequest request)
        {
            List<Employee> result = employeeService.GetAll();
            return Json(new { data = result, total = result.Count },JsonRequestBehavior.AllowGet); 
        }

        //
        // GET: /Employee/Create
        [HttpGet]
        public ActionResult Create()
        {
            Employee employee = new Employee();
            employee.Birthday = DateTime.Now;
            employee.OnboardDate = DateTime.Now;
            employee.FireDate = null;
            employee.EID = employeeService.GetNextNo();
            return View(employee);
        }

        //
        // POST: /Employee/Create

        [HttpPost]
        public ActionResult CreateAndEdit(Employee model)
        {
            try
            {
                // TODO: Add insert logic here
                if (Request.IsAjaxRequest())
                {
                    if (model.EmployeeID.ToString() == "00000000-0000-0000-0000-000000000000") //add new entity
                    {
                        model.EmployeeID = Guid.NewGuid();
                        if (ModelState.IsValid)
                        {
                            employeeService.Add(model);
                        }
                    }
                    else  //update entity
                    {
                        if (ModelState.IsValid)
                        {
                            employeeService.Update(model);
                        }
                    }
                    
                }
                return Json(new { data = true, message = "OK" }, JsonRequestBehavior.AllowGet); 
            }
            catch
            {
                return Json(new { data = false, message = "Failed" }, JsonRequestBehavior.AllowGet); 
            }
        }

        //
        // GET: /Employee/Edit/5
        [HttpGet]
        public ActionResult Edit(string id)
        {
            var employee= employeeService.GetItemByID(id);
            return View("Create",employee);
        }

        //
        // GET: /Employee/Delete/5
        public ActionResult Delete(string id)
        {
            try
            {
                employeeService.Delete(id);
                return Json(new { message = "ok" },JsonRequestBehavior.AllowGet);
            }
            catch
            {
                return Json(new { message = "no" }, JsonRequestBehavior.AllowGet);
            }
        }

    }
}
